package model;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.ck.hzq.R;

import java.util.List;

import Bean.POSSelectBean;

public class ListviewPosAdapter1 extends BaseAdapter {

    private List<POSSelectBean.DataBean.ItemsBean> data;//数据

    private Context context;//上下文

    public ListviewPosAdapter1(List<POSSelectBean.DataBean.ItemsBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.listviewpos_item, null, false);
            viewHolder = new ViewHolder();
            viewHolder.linearLayout = (LinearLayout) view.findViewById(R.id.listviewpos_linear);
            viewHolder.imageView = (ImageView) view.findViewById(R.id.listviewpos_img);
            viewHolder.textView1 = (TextView) view.findViewById(R.id.listviewpos_txt1);
            viewHolder.textView2 = (TextView) view.findViewById(R.id.listviewpos_txt2);
            viewHolder.textView3 = (TextView) view.findViewById(R.id.listviewpos_txt3);
            viewHolder.textView4 = (TextView) view.findViewById(R.id.listviewpos_txt4);
            viewHolder.textView5 = (TextView) view.findViewById(R.id.listviewpos_txt5);
            viewHolder.textView6 = (TextView) view.findViewById(R.id.listviewpos_txt6);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.textView1.setText(position + 1 + "");
        try {
            viewHolder.textView2.setText(data.get(position).getPosInstanceSn());
            if (data.get(position).getState().equals("0")) {
                viewHolder.textView3.setText("未激活");
                viewHolder.imageView.setVisibility(View.GONE);
            } else if (data.get(position).getState().equals("1")) {
                viewHolder.textView3.setText("已激活");
            } else {
                viewHolder.textView3.setText(data.get(position).getState());
            }
            viewHolder.textView4.setText("姓名:" + data.get(position).getCustomName());
            viewHolder.textView5.setText("电话:" + data.get(position).getCustomPhoneNumber());
            viewHolder.textView6.setText("费率:" + data.get(position).getRate());

        } catch (Exception e) {

        }

        final ImageView img = viewHolder.imageView;
        final LinearLayout linearLayout = viewHolder.linearLayout;

        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (linearLayout.getVisibility() == View.GONE) {
                    img.setImageResource(R.drawable.arrow_blue);
                    linearLayout.setVisibility(View.VISIBLE);
                } else {
                    img.setImageResource(R.drawable.arrow);
                    linearLayout.setVisibility(View.GONE);
                }
            }
        });

        return view;
    }

    private class ViewHolder {
        ImageView imageView;
        LinearLayout linearLayout;
        TextView textView1, textView2, textView3, textView4, textView5, textView6;
    }

}
